import { BoundingRect } from '../module/event'
import { Shape } from '../shape'
import Circle from '../shape/circle'
import Rect from '../shape/rect'

export const computeBoundingRect = (shape: Shape): BoundingRect => {
  if (shape instanceof Rect) {
    return {
      left: shape.x,
      top: shape.y,
      right: shape.x + shape.width,
      bottom: shape.y + shape.height,
      width: shape.width,
      height: shape.height,
    }
  } else if (shape instanceof Circle) {
    return {
      left: shape.center.x - shape.radius,
      top: shape.center.y - shape.radius,
      right: shape.center.x + shape.radius,
      bottom: shape.center.y + shape.radius,
      width: shape.radius * 2,
      height: shape.radius * 2,
    }
  }
  return {
    left: 0,
    top: 0,
    right: 0,
    bottom: 0,
    width: 0,
    height: 0,
  }
}

export const hasOverlap = (b1: BoundingRect, b2: BoundingRect): boolean => {
  // b1
  // b1 左上角在不在b2内部
  if (b1.left >= b2.left && b1.left <= b2.right && b1.top >= b2.top && b1.top <= b2.bottom) {
    return true
  }
  // b1 右上角在不在b2内部
  if (b1.right >= b2.left && b1.right <= b2.right && b1.top >= b2.top && b1.top <= b2.bottom) {
    return true
  }
  // b1 右下角在不在b2内部
  if (b1.right >= b2.left && b1.right <= b2.right && b1.bottom >= b2.top && b1.bottom <= b2.bottom) {
    return true
  }
  // b1 左下角在不在b2内部
  if (b1.left >= b2.left && b1.left <= b2.right && b1.bottom >= b2.top && b1.bottom <= b2.bottom) {
    return true
  }

  // b2
  // b2 左上角在不在b1内部
  if (b2.left >= b1.left && b2.left <= b1.right && b2.top >= b1.top && b2.top <= b1.bottom) {
    return true
  }
  // b2 右上角在不在b1内部
  if (b2.right >= b1.left && b2.right <= b1.right && b2.top >= b1.top && b2.top <= b1.bottom) {
    return true
  }
  // b2 右下角在不在b1内部
  if (b2.right >= b1.left && b2.right <= b1.right && b2.bottom >= b1.top && b2.bottom <= b1.bottom) {
    return true
  }
  // b2 左下角在不在b1内部
  if (b2.left >= b1.left && b2.left <= b1.right && b2.bottom >= b1.top && b2.bottom <= b1.bottom) {
    return true
  }
  return false
}
